<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginforms">
          <p class="title">
            <h1>Hello</h1>
            <h2>欢迎来到bulu后台管理系统</h2>
          </p>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :prefix-icon="User" placeholder="请输入用户名"/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
    show-password/>
          </el-form-item>
          <el-button class="login_btn" type="primary" @click="login(loginForm)" :loading="loading">登录</el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang='ts'>
  import { User, Lock } from '@element-plus/icons-vue';
  import { ref, reactive } from 'vue';
  import useUserStore from '@/store/modules/user';
  import type {loginForm} from '@/api/user/type.ts';
  import { ElNotification } from 'element-plus';
  import {useRouter} from 'vue-router';
  import { getTime } from '@/utils/time';
  let $router = useRouter();
  let userStore = useUserStore();
  let loading = ref(false)
  let loginForm = reactive({
    username: 'admin',
    password: 'atguigu123'
   })
   let loginforms = ref();
   const login = async (data:loginForm) => {
    // 表单校验
    await loginforms.value.validate()
    loading.value = true
    userStore.userLogin(loginForm).then(() => {
      // 登录成功
      loading.value = false;
      $router.push('/');
      ElNotification({
        type: 'success',
        message: '欢迎回来',
        title: `HI，${getTime()}好!`
      })
    }).catch((error) => {
      // 登录失败
      loading.value = false;
      ElNotification({
        type: 'error',
        message: error
      })
    })
   }
   const rules = {
    username: [{
      required: true,
      message: '用户名不能为空',
      trigger: 'blur'
    }, {
      required: true,
      min: 5,
      max: 10,
      message: '用户名长度至少5位',
      trigger: 'change'
    }],
    password: [{
      required: true,
      message: '密码长度在6位到15位之间',
      min: 6,
      max: 15,
      trigger: 'change'
    }]
   }

</script>

<style lang='scss' scoped>
  .login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/login_bg.webp') no-repeat;
    background-size: cover;
    color: #fff;
  }
  :deep(.el-form-item__label) {
    color: #fff;
  }
  .title {
    h1 {
      font-size: 40px;
    }
    h2 {
      font-size: 25px;
      margin: 20px 0;
    }
  }
  .login_form {
    position: absolute;
    top: 30vh;
    width: 48%;
    padding: 40px;
    box-sizing: border-box;
    background-color:#2d58c0;
    border-radius: 8px;
  }
  .login_btn {
    width: 100%;
  }
</style>